home *** CD-ROM | disk | FTP | other *** search
Text File | 1999-01-25 | 10.3 KB | 349 lines | [TEXT/CWIE] |
- ///--------------------------------------------------------------------------------------
- // SWGameUtils.c
- //
- // Portions are copyright: © 1991-94 Tony Myles, All rights reserved worldwide.
- //
- // Description: some utility functions for games
- ///--------------------------------------------------------------------------------------
-
-
- #include <QDOffscreen.h>
- #include <Palettes.h>
- #include <SWCommonHeaders.h>
- #include <SWGameUtils.h>
-
-
- ///--------------------------------------------------------------------------------------
- // Randomize - initialize random number seed
- ///--------------------------------------------------------------------------------------
-
- void Randomize( void )
- {
- GetDateTime( (unsigned long *)(&qd.randSeed) );
- }
-
-
- ///--------------------------------------------------------------------------------------
- // GetRandom - generate a random number between min and max inclusive
- ///--------------------------------------------------------------------------------------
-
- short GetRandom(short min, short max)
- {
- unsigned short random;
- long range, temp;
-
- random = Random();
- range = (max - min) + 1;
- temp = (random * range) / 65536;
- random = temp + min;
-
- return random;
- }
-
-
- ///--------------------------------------------------------------------------------------
- // CenterRect - centers rectA in rectB, returning result in rectA
- ///--------------------------------------------------------------------------------------
-
- void CenterRect(Rect* rectA, Rect* rectB)
- {
- short width = (rectA->right - rectA->left);
- short height = (rectA->bottom - rectA->top);
-
- rectA->left = rectB->left + (((rectB->right - rectB->left) / 2) - (width / 2));
- rectA->top = rectB->top + (((rectB->bottom - rectB->top) / 2) - (height / 2));
- rectA->right = rectA->left + width;
- rectA->bottom = rectA->top + height;
- }
-
-
- ///--------------------------------------------------------------------------------------
- // AlignRect - aligns the rectangle so its left side is positioned on an even long word
- // boundary. This would be every 4th pixel in 8-bit, and every 2nd in 16-bit. However,
- // we just do every 4th pixel regardless of depth, since it's easier that way. :-)
- // Besides, it means our rect will be aligned to double boundaries when in 16-bit.
- ///--------------------------------------------------------------------------------------
-
- void AlignRect(Rect* rectP)
- {
- short rectWidth, overflow;
-
- rectWidth = rectP->right - rectP->left;
- overflow = rectP->left & 3; // Save right two bits
-
- rectP->left = rectP->left>>2<<2; // Eliminate right two bits (numbers 1-3)
-
- if (overflow > 2) // Round up if closer to right than to left
- rectP->left += 4; // (just a nice touch)
-
- rectP->right = rectP->left + rectWidth;
- }
-
-
-
- short gOldEventMask; // Used by AllowKeyUpEvents and RestoreEventMask
- Boolean eventMaskIsGood = false;
-
- ///--------------------------------------------------------------------------------------
- // AllowKeyUpEvents - allows keyUpEvents. Make sure to call RestoreEventMask
- // before your program quits if you call AllowKeyUpEvents.
- ///--------------------------------------------------------------------------------------
-
- void AllowKeyUpEvents( void )
- {
- gOldEventMask = LMGetSysEvtMask();
- SetEventMask(everyEvent);
-
- // Let RestoreEventMask know that the old mask has been saved
- eventMaskIsGood = true;
- }
-
-
- ///--------------------------------------------------------------------------------------
- // RestoreEventMask - call this before your program quits if you previously
- // called AllowKeyUpEvents. This will beep if you didn't call AllowKeyUpEvents first.
- ///--------------------------------------------------------------------------------------
-
- void RestoreEventMask( void )
- {
- if (eventMaskIsGood)
- SetEventMask(gOldEventMask);
- else
- SysBeep(1);
- }
-
-
-
- ///--------------------------------------------------------------------------------------
- // Globals for HideMenuBar and ShowMenuBar
- ///--------------------------------------------------------------------------------------
-
- RgnHandle gOldVisRgn = NULL; // visRgn of window before hiding menu bar
- RgnHandle gUpdateRgn = NULL; // region returned to user
- short gOldMBarHeight;
-
-
- ///--------------------------------------------------------------------------------------
- // SWHideMenuBar - expands the vis region of grafPort to cover the entire window, which
- // will allow you to draw in the top of that window to erase the menu bar. This is a
- // simple routine designed for programs with only one window that covers the menu bar.
- // If you need to expand the region of more than one window, you need a different routine.
- // Be sure to make the window visible before calling this. HideMenuBar returns the
- // region of the menu bar and corners of the screen, in case you want to erase or
- // draw in that area. Remember to dispose this region when done with it!
- ///--------------------------------------------------------------------------------------
-
- RgnHandle SWHideMenuBar(GrafPtr grafPort)
- {
- RgnHandle newVisRgn;
- GrafPtr savePort;
-
- if (gOldVisRgn != NULL)
- return NULL;
-
- GetPort(&savePort);
- SetPort(grafPort);
-
- gOldMBarHeight = LMGetMBarHeight();
- LMSetMBarHeight( 0 ); // Keeps things like SuperClock from coming on.
-
- // save off vis region
- gOldVisRgn = NewRgn();
- CopyRgn(grafPort->visRgn, gOldVisRgn);
-
- // expand the vis region of the port rect to be completely rectangular
- newVisRgn = NewRgn();
- RectRgn(newVisRgn, &grafPort->portRect);
- CopyRgn(newVisRgn, grafPort->visRgn);
- DisposeRgn(newVisRgn);
-
- // dispose gUpdateRgn from previous call to HideMenuBar in case user forgot to
- if (gUpdateRgn != NULL)
- {
- DisposeRgn(gUpdateRgn);
- gUpdateRgn = NULL;
- }
-
- // set gUpdateRgn to region of rounded corners and menu bar
- gUpdateRgn = NewRgn();
- CopyRgn(gOldVisRgn, gUpdateRgn);
- DiffRgn(grafPort->visRgn, gUpdateRgn, gUpdateRgn);
-
- SetPort(savePort);
- return gUpdateRgn;
- }
-
-
- ///--------------------------------------------------------------------------------------
- // KeepMenuBarHidden - call this during your main animation loop to make sure the
- // window's visRgn stays the same as the window's portRect. (Utilities like the Control
- // strip can mess it up.) Only necessary if you use CopyBits as your screenDrawProc,
- // and you don't hide the Control Strip before starting your animation.
- ///--------------------------------------------------------------------------------------
-
- void KeepMenuBarHidden(GrafPtr grafPort)
- {
- Rect regionRect;
-
- regionRect = (**grafPort->visRgn).rgnBBox;
-
- // Fix the region only if it needs fixing
- if (regionRect.top != grafPort->portRect.top ||
- regionRect.bottom != grafPort->portRect.bottom ||
- regionRect.left != grafPort->portRect.left ||
- regionRect.right != grafPort->portRect.right )
- {
- RectRgn(grafPort->visRgn, &grafPort->portRect);
- }
- }
-
-
- ///--------------------------------------------------------------------------------------
- // SWShowMenuBar - restores the grafPort to the way it was before the call to HideMenuBar.
- // Make sure to call this after every call to HideMenuBar to dispose of gOldVisRgn.
- ///--------------------------------------------------------------------------------------
-
- void SWShowMenuBar(GrafPtr grafPort)
- {
- GrafPtr savePort;
- RgnHandle junkRgn;
-
- if (gOldVisRgn == NULL)
- return;
-
- GetPort(&savePort);
- SetPort(grafPort);
-
- LMSetMBarHeight( gOldMBarHeight );
-
- // fill the rounded corners of the screen with black again
- junkRgn = NewRgn();
- CopyRgn(gOldVisRgn, junkRgn);
- DiffRgn(grafPort->visRgn, junkRgn, junkRgn);
- ForeColor(blackColor);
-
- FillRgn(junkRgn, &qd.black);
-
- DisposeRgn(junkRgn);
-
- // restore the old vis region
- CopyRgn(gOldVisRgn, grafPort->visRgn);
- DisposeRgn(gOldVisRgn);
- gOldVisRgn = NULL;
-
- // Dispose this region which was created by SWHideMenuBar
- if (gUpdateRgn != NULL)
- {
- DisposeRgn(gUpdateRgn);
- gUpdateRgn = NULL;
- }
-
- DrawMenuBar();
-
- SetPort(savePort);
- }
-
-
- ///--------------------------------------------------------------------------------------
- // GetDepthFromGlobalRect
- ///--------------------------------------------------------------------------------------
-
- short GetDepthFromGlobalRect( Rect* globalRect )
- {
- SysEnvRec theSERec;
- GDHandle rectGDH;
- short depth;
-
-
- SysEnvirons( 2, &theSERec );
- if ( !theSERec.hasColorQD ) // no colorQD?
- {
- depth = 1;
- }
- else
- {
- rectGDH = GetMaxDevice( globalRect );
- depth = GetGDeviceDepth( rectGDH );
- }
- return depth;
- }
-
-
- ///--------------------------------------------------------------------------------------
- // GetDepthFromWindow
- ///--------------------------------------------------------------------------------------
-
- short GetDepthFromWindow( WindowPtr theWindow )
- {
- SysEnvRec theSERec;
- GWorldPtr oldGWorld, windowGWld;
- GDHandle oldGDH, windowGDH;
- short depth;
-
-
- SysEnvirons( 2, &theSERec );
- if ( !theSERec.hasColorQD ) // no colorQD?
- depth = 1;
- else
- {
- GetGWorld( &oldGWorld, &oldGDH );
- SetPort( theWindow );
- GetGWorld( &windowGWld, &windowGDH );
-
- depth = GetGDeviceDepth( windowGDH );
-
- SetGWorld( oldGWorld, oldGDH );
- }
- return depth;
- }
-
-
- ///--------------------------------------------------------------------------------------
- // GetGDeviceDepth - to get the depth of the main monitor,
- // simply do this: GetGDeviceDepth( GetMainDevice() );
- ///--------------------------------------------------------------------------------------
-
- short GetGDeviceDepth( GDHandle theGDH )
- {
- SysEnvRec theSERec;
- PixMapHandle thePixMap;
-
-
- SysEnvirons( 2, &theSERec );
- if ( !theSERec.hasColorQD ) // no colorQD?
- return 1;
- else
- {
- thePixMap = (**theGDH).gdPMap;
- return (**thePixMap).pixelSize; // Depth in bits (1,2,4...)
- }
- }
-
-
- ///--------------------------------------------------------------------------------------
- // RestoreSystemPalette - if your game uses a custom palette, call this when switching
- // to the Finder or some other app if you want it to have its original palette back.
- ///--------------------------------------------------------------------------------------
-
- void RestoreSystemPalette( void )
- {
- RestoreDeviceClut(nil);
- PaintBehind(FrontWindow(), GetGrayRgn());
- PaintOne(nil, GetGrayRgn());
- DrawMenuBar();
- }
-
-
- ///--------------------------------------------------------------------------------------
- // HasSystem7
- ///--------------------------------------------------------------------------------------
-
- Boolean HasSystem7( void )
- {
- long gestaltResponse;
- OSErr err;
-
- err = Gestalt( gestaltSystemVersion, &gestaltResponse );
- return ( err == noErr && (gestaltResponse >= 0x0700));
- }
-